<template>
  <div class="login">
    <el-form size="medium" :inline="true" :label-position="labelPosition" label-width="60px" :model="form">
      <el-form-item label="账号">
        <el-input v-model="form.user"></el-input>
      </el-form-item>
      <br />
      <el-form-item label="密码">
        <el-input v-model="form.password" type="password"></el-input>
      </el-form-item>
      <br />
      <drag-verify
        :width="drag.width"
        :height="drag.height"
        :text="drag.text"
        :success-text="drag.successText"
        :text-size="drag.textSize"
        class="darg"
        @passcallback="verified = true"
      >
      </drag-verify>
      <el-form-item class="button">
        <el-button type="primary" @click="dialogVisible = true" size="small" :disabled="!verified">注册</el-button>
        <el-button type="primary" @click="onLogin" size="small" :disabled="!verified">登陆</el-button>
      </el-form-item>
    </el-form>

    <!-- 注册用户的对话框 -->
    <el-dialog title="注册用户" :visible.sync="dialogVisible" width="500px">
      <!-- 内容的主体区域 -->
      <el-form ref="addFormRef" :model="addForm" :rules="addFormRules" label-width="80px">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="用户密码" prop="password">
          <el-input v-model="addForm.password" type="password"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="surePassword">
          <el-input v-model="addForm.surePassword" type="password"></el-input>
        </el-form-item>
        <el-form-item label="收货地址" prop="address">
          <area-cascader
            class="area-cascader-wrap"
            :level="1"
            type="text"
            placeholder="请选择地区"
            v-model="address"
            :data="pcaa"
          ></area-cascader>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="addForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="用户邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onRegister">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import dragVerify from 'vue-drag-verify'
import 'vue-area-linkage/dist/index.css'
import { pca, pcaa } from 'area-data'

export default {
  data() {
    return {
      // 选择的地址
      address: '',
      pca: pca,
      pcaa: pcaa,
      drag: {
        width: 300,
        height: 48,
        text: '请拖动到右侧进行验证',
        successText: '验证成功',
        textSize: '16px'
      },

      labelPosition: 'left',
      form: {
        user: '',
        password: ''
      },

      verified: false,
      dialogVisible: false,
      // 添加用户的表单数据
      addForm: {
        username: '',
        password: '',
        surePassword: '',
        picUrl: '',
        address: '',
        phone: '',
        email: ''
      },
      // 添加表单的验证规则对象
      addFormRules: {
        username: [
          { required: true, message: '请输入用户名称', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入用户密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        surePassword: [
          { required: true, message: '请重新输入用户密码', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        picUrle: [{ required: true, message: '请输入用户头像', trigger: 'blur' }],
        address: [{ required: true, message: '请输入收货地址', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入联系方式', trigger: 'blur' }],
        email: [{ required: true, message: '请输入用户邮箱', trigger: 'blur' }]
      }
    }
  },
  watch:{
    address:function(value){
      this.addForm.address = value.join('-')
    }
  },
  components: {
    dragVerify
  },
  methods: {
    onLogin() {
      this.$store.dispatch('user/login', {
        username: this.form.user,
        password: this.form.password
      })
    },
    // 注册
    onRegister() {
      var that = this
      if (this.addForm.password != this.addForm.surePassword) {
        this.$message.error('两次输入的密码不同,请重新输入!')
      } else {
        var url =
          '/user/register?username=' +
          this.addForm.username +
          '&password=' +
          this.addForm.password +
          '&picUrl=' +
          this.addForm.picUrl +
          '&address=' +
          this.addForm.address +
          '&phone=' +
          this.addForm.phone +
          '&email=' +
          this.addForm.email
        this.$axios
          .get(url, {
            username: this.addForm.username,
            password: this.addForm.password,
            picUrl: this.addForm.picUrl,
            address: this.addForm.address,
            phone: this.addForm.phone,
            email: this.addForm.email
          })
          .then(function(response) {
            console.log(response);
            if (response.data.code == '000') {
              //alert('注册成功，请登陆！');
              that.$message.success('注册成功，请登陆！')
              // 刷新页面
              window.location.reload(true)
            } else {
              //alert('账号已存在，请登陆账号或修改账号重新注册！');
              that.$message.error('账号已存在，请登陆账号或修改账号重新注册!')
              // 刷新页面
              window.location.reload(true)
            }
          })
          .catch(function(error) {
            console.log(error)
          })
      }
    }
  }
}
</script>

<style scoped lang="less">
 .area-cascader-wrap {
  width: 100%;
  ::v-deep.large {
    width: 100%;
    line-height: 20px;
  }
}
.login {
  width: 400px;
  height: 200px;
  margin: 0 auto;
  margin-top: 80px;
  margin-bottom: 150px;
  text-align: center;
  padding: 40px;

  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  border-radius: 10px;
}

.darg {
  margin: 0 auto;
  margin-bottom: 20px;
}

.button {
  float: right;
  margin-right: 75px !important;
}
</style>
